namespace Xsxiaosa.Net
{
    using System;
    using System.Net;
    using System.Net.Sockets;

    internal class ProxyNone : ProxyBase
    {
        // Methods
        public ProxyNone(Socket s) : base(s)
        {
        }

        public override IAsyncResult BeginAccept(AsyncCallback callback, object state)
        {
            Socket socket1;
            base.Begin("Accept", callback, state);
            try
            {
                socket1 = base.Socket;
                base.SetSocket(socket1.Accept());
                socket1.Close();
            }
            catch (Exception exception1)
            {
                base.SetError(exception1);
            }
            finally
            {
                base.Finish();
            }
            return this._asyncResult;
        }

        public override IAsyncResult BeginConnect(IPEndPoint endPoint, AsyncCallback callback, object state)
        {
            base.Begin("Connect", callback, state);
            base.Socket.BeginConnect(endPoint, new AsyncCallback(this.Connect1), null);
            return this._asyncResult;
        }

        public override IAsyncResult BeginConnect(string serverName, int serverPort, AsyncCallback callback, object state)
        {
            base.Begin("Connect", callback, state);
          
            Dns.BeginGetHostAddresses(serverName, new AsyncCallback(this.Connect0), serverName);
            return this._asyncResult;
        }

        public override IAsyncResult BeginListen(ProxyBase controlSocket, AsyncCallback callback, object state)
        {
            IPEndPoint point1;
            base.Begin("Listen", callback, state);
            try
            {
                point1 = new IPEndPoint(((IPEndPoint) controlSocket.Socket.LocalEndPoint).Address, 0);
                base.Socket.Bind(point1);
                base.Socket.Listen(0);
                this._asyncResult.SetResult(((IPEndPoint) base.Socket.LocalEndPoint));
            }
            catch (Exception exception1)
            {
                base.SetError(exception1);
            }
            finally
            {
                base.Finish();
            }
            return this._asyncResult;
        }

        private void Connect0(IAsyncResult result)
        {
            int num1;
            IPHostEntry entry1;
            IPEndPoint point1;
            try
            {
                num1 = ((int) result.AsyncState);
                try
                {
                    entry1 = Dns.EndResolve(result);
                }
                catch (SocketException exception1)
                {
                    base.SetError(new ProxySocketException(Messages.GetString(typeof(ProxySocketStrings), "ResolveFailed"), ProxySocketExceptionStatus.NameResolutionFailure, exception1));
                    base.Finish();
                    return;
                }
                point1 = new IPEndPoint(entry1.AddressList[0], num1);
                base.Socket.BeginConnect(point1, new AsyncCallback(this.Connect1), null);
            }
            catch (Exception exception2)
            {
                base.SetError(exception2);
                base.Finish();
            }
        }

        private void Connect1(IAsyncResult asyncResult)
        {
            try
            {
                base.Socket.EndConnect(asyncResult);
            }
            catch (Exception exception1)
            {
                base.SetError(new ProxySocketException(exception1.Message, ProxySocketExceptionStatus.ConnectFailure, exception1));
            }
            finally
            {
                base.Finish();
            }
        }

    }}

